package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.LoginInfo
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.ILoginView
import de.baumann.browser.model.UserModel
import de.baumann.browser.utils.*
import de.baumann.browser.utils.UnicornUtil.Companion.setUserInfo

class LoginPresenter : BasePresenter<ILoginView>() {

    private val userModel: UserModel = UserModel()

    fun login() {
        userModel.login(view.getAccount(), view.getPassword())
                .subscribe(object : OdinObserver<LoginInfo>(view) {
                    override fun onSuccess(loginInfo: LoginInfo) {
                        saveUser(loginInfo)
                        setUserInfo(loginInfo)
                        saveInviteCode(loginInfo.inviteCode)
                        saveInviteCodeStatus(loginInfo.inviteId)
                        saveFormAddress(loginInfo.fromAddress)
                        view?.loginSuccess(loginInfo.loginName, loginInfo.lisenceStatus)
                        saveLogin(true)
                    }
                })
    }

}
